import * as types from '../mutation-types';
import Vue from 'vue';
const deleteOption = {
	text: '删除',
	mapping: 'deleteSession',
	style: {
		backgroundColor: '#dd524d'
	}
};
const readOption = {
	text: '已读',
	mapping: 'readSession',
	style: {
		backgroundColor: '#007aff'
	}
};

const state = {
	singleSessionList: [],
	groupSessionList: [],
	noticeList: [],
	itemUnread: {
		singleSession: 0,
		groupSession: 0,
		notice: 0,

	},
	totalUnread: 0
}

const mutations = {
	[types.GET_SINGLE_SESSION_LIST](state, callback) {
		Vue.prototype.$u.api.getSingleChatSessionList().then(res => {
			let unreadCount = 0;
			const singleSessionList = res.data.data;
			state.singleSessionList = singleSessionList.map(item => {
				item.options = [deleteOption];
				if (item.chat_unread > 0) {
					item.options.unshift(readOption)
					unreadCount += item.chat_unread
				}
				item.show = false;
				return item;
			});
			this.commit(types.UPDATE_UNREAD_COUNT, { type: 'singleSession', count: (unreadCount || 0) })
			callback && callback()
		}).catch(e => {
			callback && callback()
		})
	},
	[types.SHOW_SINGLE_SESSION_LIST_SWIPE_ACTION](state, index = -1) {
		state.singleSessionList.map((val, i) => {
			Vue.set(state.singleSessionList[i], 'show', index == i);
		});

	},
	[types.SET_SINGLE_SESSION_READ](state, toIds = '') {
		let unreadCount = 0;
		const toIdList = toIds.split(',');
		for (let i = 0; i < toIdList.length; i++) {
			for (let j = 0; j < state.singleSessionList.length; j++) {
				if (state.singleSessionList[j].chat_user == toIdList[i]) {
					Vue.set(state.singleSessionList[j], 'chat_unread', 0)
					break;
				}
			}
		}

		state.singleSessionList = state.singleSessionList.map(item => {
			item.options = [deleteOption];
			if (item.chat_unread > 0 && !item.is_delete) {
				item.options.unshift(readOption)
				unreadCount += item.chat_unread
			}
			item.show = false;
			return item;
		});
		this.commit(types.UPDATE_UNREAD_COUNT, { type: 'singleSession', count: (unreadCount || 0) })
	},
	[types.DELETE_SINGLE_SESSION_ITEM](state, sessionIds = '') {
		let unreadCount = 0;
		const sessionIdList = sessionIds.split(',');
		for (let i = 0; i < sessionIdList.length; i++) {
			for (let j = 0; j < state.singleSessionList.length; j++) {
				if (state.singleSessionList[j].notice_id == sessionIdList[i]) {
					Vue.set(state.singleSessionList[j], 'is_delete', 1)
					break;
				}
			}
		}

		state.singleSessionList = singleSessionList.map(item => {
			item.options = [deleteOption];
			if (!item.is_delete) {
				unreadCount += item.chat_unread
			}
			item.show = false;
			return item;
		});
		this.commit(types.UPDATE_UNREAD_COUNT, { type: 'singleSession', count: (unreadCount || 0) })
	},
	[types.SET_GROUP_SESSION_READ](state, gids = '') {
		let unreadCount = 0
		const gidList = gids.split(',');
		for (let i = 0; i < gidList.length; i++) {
			for (let j = 0; j < state.groupSessionList.length; j++) {
				if (state.groupSessionList[j].chat_user == gidList[i]) {
					Vue.set(state.groupSessionList[j], 'chat_unread', 0)
					break;
				}
			}
		}
		state.groupSessionList.map(item => {
			unreadCount += item.chat_unread || 0;
		});
		this.commit(types.UPDATE_UNREAD_COUNT, { type: 'groupSession', count: (unreadCount || 0) })
	},

	[types.GET_NOTICE_LIST](state, callback) {
		Vue.prototype.$u.api.getOfficialNoticeHome().then(res => {
			let unreadCount = 0
			state.noticeList = res.data.type_list.filter(item => {
				unreadCount += item.unread_count || 0;
				return item.last.length > 0
			});
			this.commit(types.UPDATE_UNREAD_COUNT, { type: 'notice', count: (unreadCount || 0) })
			callback && callback()
		});
	},
	[types.SET_NOTICE_READ](state, ids = '') {
		let unreadCount = 0
		const idList = ids.split(',');
		for (let i = 0; i < idList.length; i++) {
			for (let j = 0; j < state.noticeList.length; j++) {
				if (state.noticeList[j].type_id == idList[i]) {
					Vue.set(state.noticeList[j], 'unread_count', 0)
					break;
				}
			}
		}
		state.noticeList.map(item => {
			unreadCount += item.unread_count || 0;
		});
		this.commit(types.UPDATE_UNREAD_COUNT, { type: 'notice', count: (unreadCount || 0) })
	},
	[types.UPDATE_UNREAD_COUNT](state, data) {
		console.log(data)
		let totalUnread = 0
		if (state.itemUnread.hasOwnProperty(data.type)) {
			Vue.set(state.itemUnread, data.type, data.count)
		}

		for (let key in state.itemUnread) {
			totalUnread += state.itemUnread[key] || 0;
		}
		if (totalUnread <= 0) {
			uni.removeTabBarBadge({
				index: 1
			})
		} else {
			uni.setTabBarBadge({
				index: 1,
				text: String(totalUnread > 99 ? '...' : totalUnread)
			})
		}
		state.totalUnread = totalUnread
	}


}

const actions = {
	[types.GET_GROUP_SESSION_LIST]({ state, commit }, page) {
		return new Promise((resolve, reject) => {
			Vue.prototype.$u.api.getGroupChatSessionList(page.num, page.size).then(res => {
				let unreadCount = 0;
				if (page.num == 1) {
					state.groupSessionList = res.data.data
				} else {
					state.groupSessionList.push(res.data.data)
				}

				state.groupSessionList.map(item => {
					if (item.chat_unread > 0) {
						unreadCount += item.chat_unread
					}
				})
				commit(types.UPDATE_UNREAD_COUNT, { type: 'groupSession', count: (unreadCount || 0) })
				resolve(res);
			}).catch(e => {
				reject(e);
			})
		})
	},
}

export default {
	state,
	actions,
	mutations
}
